# Maintainer: Florian Pritz <bluewind@xinu.at>
# Contributor: Eric Bélanger <eric@archlinux.org>

pkgname=syslog-ng
pkgver=4.4.0
pkgrel=3
pkgdesc="Next-generation syslogd with advanced networking and filtering capabilities"
arch=('loong64' 'x86_64')
url="https://www.syslog-ng.com/products/open-source-log-management/"
license=('GPL2' 'LGPL2.1')
depends=(
  'awk'
  'bash'
  'curl'
  'glib2'
  'glibc'
  'grpc'
  'json-c'
  'libcap'
  'libnet'
  'libnsl'
  'openssl'
  'pcre2'
  'protobuf'
  'systemd-libs'
)
makedepends=('libxslt' 'mongo-c-driver' 'librabbitmq-c' 'python' 'libesmtp' 'hiredis'
             'libdbi' 'libmaxminddb' 'net-snmp' 'librdkafka' 'systemd') #'libcap'
makedepends+=('python-nose' 'python-ply')
optdepends=('logrotate: for rotating log files'
            'libdbi: for the SQL plugin'
            'librabbitmq-c: for the AMQP plugin'
            'mongo-c-driver: for the MongoDB plugin'
            'libesmtp: for the SMTP plugin'
            'hiredis: for the Redis plugin'
            'libmaxminddb: for the GeoIP2 plugin'
            'net-snmp: for the SNMP plugin'
            'librdkafka: for the Kafka C plugin'
            'python: for Python-based plugins'
            'python-ply: for debugger CLI')
conflicts=('eventlog')
replaces=('eventlog')
# The default scl.conf moved in 4.3.0, but it is still supported here for overrides so we keep this entry. See https://github.com/syslog-ng/syslog-ng/pull/4534 for details.
backup=('etc/syslog-ng/scl.conf'
        'etc/syslog-ng/syslog-ng.conf'
        'etc/logrotate.d/syslog-ng'
        'etc/default/syslog-ng@default')
source=(https://github.com/balabit/syslog-ng/releases/download/syslog-ng-$pkgver/$pkgname-$pkgver.tar.gz
        syslog-ng.conf syslog-ng.logrotate)
sha512sums=('1cf18c43321e523d0f49b25a4839fd10ec301b207da1b2c407df5167dba557ac0a7781b07f1c1e5d18241376966b8099d9e41dffa55030f6a8052388139c4107'
            'cbcc5f6e6d0ed75add8a38bf35a5bee9f0cce487e0eda76d34bf807e1fe1f49bd86db707c7457205e502f0c7de78f44c5b9cb8a8e993880095089e75248906c2'
            'cd39f545a6a855c866a466bf846e33940b2c2dd1fc2eaf50cce29c68e1a5753c7c4b56411e4f01c152f32e155104a98dd755a96319767f47c73a8853f720b2cc')

prepare() {
  cd $pkgname-$pkgver
  sed -i -e 's,/bin/,/usr/bin/,' -e 's,/sbin/,/bin/,' contrib/systemd/syslog-ng@.service
  sed -i -e 's|/var/run|/run|g' contrib/systemd/syslog-ng@default
}

build() {
  cd $pkgname-$pkgver

  # https://archlinux.org/todo/lto-fat-objects/
  export CFLAGS+=" -ffat-lto-objects"
  export CXXFLAGS+=" -ffat-lto-objects"


  _source_version=$(sed -rn 's#.*define VERSION_STR_CURRENT.* "(.*)"#\1#p' lib/versioning.h)
  _config_version=$(head -1 "$srcdir/syslog-ng.conf" | cut -d\  -f2)

  if [[ "$_source_version" != "$_config_version" ]]; then
	  echo "Version in example config at '$srcdir/syslog-ng.conf' is: $_config_version"
	  echo "Expected version from code is: $_source_version"
	  echo "Check if config format has incompatible changes and adjust the"
	  echo "example config file and bump the version to continue the build."
	  echo "Usually simply bumping is fine since our config is simple enough"
	  return 1
  fi

  ./autogen.sh
  ./configure --prefix=/usr --sysconfdir=/etc/syslog-ng --libexecdir=/usr/lib \
    --sbindir=/usr/bin --localstatedir=/var/lib/syslog-ng --datadir=/usr/share \
    --with-pidfile-dir=/run --enable-spoof-source --enable-ipv6 \
    --enable-systemd --with-systemdsystemunitdir=/usr/lib/systemd/system \
    --enable-manpages --enable-all-modules --disable-java --disable-java-modules  \
    --disable-mqtt --disable-riemann --with-python=3 --with-jsonc=system
  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make
}

# TODO: package criterion
# check() {
#   cd $pkgname-$pkgver
#   make check
# }

package() {
  make -C $pkgname-$pkgver DESTDIR="$pkgdir" install
  install -dm755 "$pkgdir/var/lib/syslog-ng" "$pkgdir/etc/syslog-ng/patterndb.d"
  install -Dm644 "$srcdir/syslog-ng.conf" "$pkgdir/etc/syslog-ng/syslog-ng.conf"
  install -Dm644 "$srcdir/syslog-ng.logrotate" "$pkgdir/etc/logrotate.d/syslog-ng"
  install -Dm644 "$srcdir"/$pkgname-$pkgver/contrib/systemd/syslog-ng@default -t "$pkgdir"/etc/default
}
